home *** CD-ROM | disk | FTP | other *** search
/ PC User 2002 July / Disc 1 / PCU0702CD1.iso / software / sfeflash / flies / en-us / ps_0 / regdb_updatecust.cgi < prev    next >
Encoding:
Text File  |  2002-05-17  |  13.5 KB  |  458 lines

  1. #!/usr/local/bin/perl
  2.  
  3. use DBI;
  4. use CGI qw/:standard :escape :unescape/;
  5. use LWP::UserAgent;
  6.  
  7. $mailprog = '/bin/sendmail';
  8.  
  9. $register_url = 'http://www.3d3.com/encrypt/regdb_register.cgi';
  10.  
  11. $email_alert = 'tech@3d3.com';                    # email in case of fatal error
  12. $email_contact = 'support@3d3.com';                # email address to give to client
  13. $email_regdb = 'tech@3d3.com';                    # email of person who processes registrations
  14.  
  15. $database = 'regdb';
  16. $user = 'x3d3';
  17. $passwd = '4dcubed';
  18.  
  19. # Start returning a page...
  20. print "Content-type: text/html\n\n";
  21.  
  22. @required =(
  23.     'code',
  24.     'customername',
  25.     'customerstreet',
  26.     'customercity',
  27.     'customerzip',
  28.     'customercountry',
  29.     'customerphone',
  30.     'customeremail'
  31. );
  32.  
  33. my($code) = param('code');
  34. my($product) = param('product');
  35. my($version) = param('version');
  36. my($mversion) = param('mversion');
  37. my($language) = param('language');
  38. my($unlocker) = param('unlocker');
  39. my($customername) = param('customername');
  40. my($customercompany) = param('customercompany');
  41. my($customerstreet) = param('customerstreet');
  42. my($customercity) = param('customercity');
  43. my($customerstate) = param('customerstate');
  44. my($customerzip) = param('customerzip');
  45. my($customercountry) = param('customercountry');
  46. my($customerphone) = param('customerphone');
  47. my($customerfax) = param('customerfax');
  48. my($customeremail) = param('customeremail');
  49.  
  50. my($coc) = param('coc');
  51. my($oldcode) = param('oldcode');
  52.  
  53. if($coc) {
  54.     push(@required, 'oldcode');
  55. }
  56.  
  57. # check required fields
  58. &check_required;
  59.  
  60. # connect to the database
  61. $dbh = DBI->connect("DBI:mysql:$database", $user, $passwd);
  62. if($dbh) {
  63.  
  64.     my($ok) = 1;
  65.  
  66.     # are we checking the old code?  ie: is this a distributor-sold upgrade?
  67.  
  68.     if($coc) {
  69.         # yes...
  70.         if(&check_old_code($dbh, $code, $product, $version, $oldcode)) {
  71.             $ok = 1;
  72.         } else {
  73.             $ok = 0;
  74.         }
  75.     }
  76.  
  77.     if($ok) {
  78.         if(&check_code($dbh, $code)) {
  79.             my($sql) = "UPDATE Customer SET";
  80.             $sql .= " CustomerName=" . $dbh->quote($customername) . ",";
  81.             $sql .= " CustomerCompany=" . $dbh->quote($customercompany) . ",";
  82.             $sql .= " CustomerStreet=" . $dbh->quote($customerstreet) . ",";
  83.             $sql .= " CustomerCity=" . $dbh->quote($customercity) . ",";
  84.             $sql .= " CustomerState=" . $dbh->quote($customerstate) . ",";
  85.             $sql .= " CustomerZip=" . $dbh->quote($customerzip) . ",";
  86.             $sql .= " CustomerCountry=" . $dbh->quote($customercountry) . ",";
  87.             $sql .= " CustomerPhone=" . $dbh->quote($customerphone) . ",";
  88.             $sql .= " CustomerFax=" . $dbh->quote($customerfax) . ",";
  89.             $sql .= " CustomerEmail=" . $dbh->quote($customeremail);
  90.             $sql .= " WHERE Code=" . $dbh->quote($code);
  91.             my($sth) = $dbh->prepare($sql);
  92.             if($sth) {
  93.                 if($sth->execute) {
  94.                     $ok = 1;
  95.                 } else {
  96.                     &print_html('Error: $sth->execute() Failed', $sth->errstr);
  97.                 }
  98.             } else {
  99.                 &print_html('Error: $dbh->prepare() Failed', $dbh->errstr);
  100.             }
  101.  
  102.             if($ok) {
  103.                 $ok = 0;
  104.                 $sql = "UPDATE Reg SET";
  105.                 $sql .= " Name=" . $dbh->quote($customername) . ",";
  106.                 $sql .= " Email=" . $dbh->quote($customeremail) . ",";
  107.                 $sql .= " RequestCust=0";
  108.                 $sql .= " WHERE Code=" . $dbh->quote($code);
  109.                 my($sth) = $dbh->prepare($sql);
  110.                 if($sth) {
  111.                     if($sth->execute) {
  112.                         $ok = 1;
  113.                     } else {
  114.                         &print_html('Error: $sth->execute() Failed', $sth->errstr);
  115.                     }
  116.                 } else {
  117.                     &print_html('Error: $dbh->prepare() Failed', $dbh->errstr);
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123.     $dbh->disconnect;
  124.  
  125.     if($ok) {
  126.         # It's ok, give the unlock code
  127.         my($url);
  128.         $url  = "$register_url";
  129.         $url .= "?code=" . escape($code);
  130.         $url .= "&email=" . escape($customeremail);
  131.         $url .= "&product=" . escape($product);
  132.         $url .= "&version=" . escape($version);
  133.         $url .= "&mversion=" . escape($mversion);
  134.         $url .= "&language=" . escape($language);
  135.         $url .= "&unlocker=" . escape($unlocker);
  136.         $url .= "&bgcolor=" . escape(param('bgcolor'));
  137.         $url .= "&text_color" . escape(param('text_color'));
  138.         $url .= "&link_color" . escape(param('link_color'));
  139.         $url .= "&vlink_color" . escape(param('vlink_color'));
  140.         $url .= "&alink_color" . escape(param('alink_color'));
  141.  
  142.         my($ua) = new LWP::UserAgent;
  143.         my($req) = new HTTP::Request GET => $url;
  144.         my($res) = $ua->request($req);
  145.         if($res->is_success) {
  146.             print $res->content;
  147.         } else {
  148.             &print_html('Fatal Error: Can\'t Redirect To regdb_register.cgi');
  149.             &send_error_report('Can\'t Redirect To regdb_register.cgi');
  150.         }
  151.     }
  152. } else {
  153.     &print_html('Error: $dbh->connect() Failed', "Unable to connect to database - $dbh->errstr");
  154. }
  155.  
  156. sub check_old_code {
  157.     my($dbh, $code, $product, $version, $oldcode) = @_;
  158.     my($ok) = 0;
  159.  
  160.     my($qcode) = $dbh->quote($code);
  161.     my($qoldcode) = $dbh->quote($oldcode);
  162.  
  163.     my($sql) =<<"__EOSQL__";
  164. SELECT
  165.     RegOld.Code as OldCode, RegOld.ProductID as OldProductID, RegOld.Version as OldVersion,
  166.     RegNew.Code as NewCode, RegNew.ProductID as NewProductID, RegNew.Version as NewVersion,
  167.     RegNew.Upgrade,
  168.     ProdOld.Name as OldName, ProdNew.Name as NewName
  169. FROM
  170.     Reg as RegOld, Reg as RegNew, Product as ProdOld, Product as ProdNew
  171. WHERE
  172.     RegOld.ProductID = ProdOld.ProductID AND
  173.     RegNew.ProductID = ProdNew.ProductID AND
  174.     RegOld.Code = $qoldcode AND
  175.     RegNew.Code = $qcode
  176. __EOSQL__
  177.  
  178.     my($sth) = $dbh->prepare($sql);
  179.     if($sth) {
  180.         if($sth->execute) {
  181.             my($hashref) = $sth->fetchrow_hashref;
  182.  
  183.             if($hashref) {
  184.  
  185.                 # At this point we have:
  186.                 #    Code, Product, Version and Name of old product
  187.                 #    Code, Product, Version, Name, and Upgrade String of new product.
  188.                 # The Upgrade string is in the form "OldProd OldVer to NewProd NewVer".
  189.  
  190.                 my($u_oprod, $u_over, $u_nprod, $u_nver) = ($$hashref{'Upgrade'} =~ /^(\w+)\s+(\w+)\s+\Qto\E\s+(\w+)\s+(\w+)/);
  191.  
  192.                 if( ($u_oprod eq $$hashref{'OldName'}) && ($u_over eq $$hashref{'OldVersion'}) ) {
  193.                     # The old product and version from the upgrade string match the product and version from the
  194.                     # old code.  Note that as this is meant to deal with distributor-sold codes, we are ok only looking
  195.                     # at the first matched product, as there will only ever be one product per code.
  196.  
  197.                     # Note that we do not as yet require that the previous product be registered - it must only exist.
  198.  
  199.                     # At this point, we should remove the old record, so that it can't be upgraded-from again.
  200.                     # However, this would mean that the legitimate owner would not be able to request an unlock code
  201.                     # more than once...
  202.  
  203.                     $ok = 1;
  204.  
  205.                 } else {
  206.                     # The old product and version from the upgrade string do not match the product and version from
  207.                     # the old code.
  208.                     my($htmlbody) = <<"__EOHTML__";
  209. <p>The Upgrade you are registering is from $$hashref{'Upgrade'}.  The product specified by the Code you entered from your
  210. previous version of ShopFactory is "$$hashref{'OldName'} $$hashref{'OldVersion'}", and thus you cannot register
  211. this upgrade.</p>
  212. <p>Please use your browser's back button to return to the previous page, check the code you entered, and try again.</p>
  213. __EOHTML__
  214.                     &print_html('Invalid Previous Code', $htmlbody);
  215.                 }
  216.  
  217.             } else {
  218.                 my($htmlbody) = <<"__EOHTML__";
  219. <p>The Code you entered from your previous version of ShopFactory was not found
  220. in our database.  Please use your browser's back button to return to the
  221. previous page, check your code and try again.</p>
  222. __EOHTML__
  223.                 &print_html('Previous Code Does Not Exist', $htmlbody);
  224.             }
  225.  
  226.             $sth->finish;
  227.         } else {
  228.             &print_html('Error: $sth->execute() Failed', $sth->errstr);
  229.         }
  230.     } else {
  231.         &print_html('Error: $dbh->prepare() Failed', $dbh->errstr);
  232.     }
  233.     return($ok);
  234. }
  235.  
  236. sub check_code {
  237.     my($dbh, $code) = @_;
  238.     my($ok) = 0;
  239.     my($exists) = 0;
  240.  
  241.     my($sql) = "SELECT * FROM Customer WHERE Code='$code'";
  242.     my($sth) = $dbh->prepare($sql);
  243.     if($sth) {
  244.         if($sth->execute) {
  245.             if($sth->rows > 0) {
  246.                 $exists = 1;
  247.                 $ok = 1;
  248.             }
  249.             $sth->finish;
  250.         } else {
  251.             &print_html('Error: $sth->execute() Failed', $sth->errstr);
  252.         }
  253.     } else {
  254.         &print_html('Error: $dbh->prepare() Failed', $dbh->errstr);
  255.     }
  256.  
  257.     if(!$exists) {
  258.         $sql = "INSERT INTO Customer (Code) VALUES ('$code')";
  259.         $sth = $dbh->prepare($sql);
  260.         if($sth) {
  261.             if($sth->execute) {
  262.                 $ok = 1;
  263.             } else {
  264.                 &print_html('Error: $sth->execute() Failed', $sth->errstr);
  265.             }
  266.         } else {
  267.             &print_html('Error: $dbh->prepare() Failed', $dbh->errstr);
  268.         }
  269.     }
  270.     return($ok);
  271. }
  272.  
  273. sub check_required {
  274.  
  275.     if( !(param('code')) || param('code') eq ' ') {
  276.         &print_html("Fatal Error: Code Not Specified", "<p>Your Code has not be passed through correctly.</p>");
  277.         &send_error_report('Code Not Specified');
  278.         exit;
  279.     } else {
  280.         my($missing) = 0;
  281.         foreach $require (@required) {
  282.             if (!(param($require)) || param($require) eq ' ') {
  283.                 $missing = 1;
  284.                 last;
  285.             }
  286.         }
  287.         if($missing) {
  288.  
  289.             my($bgcolor) = param('bgcolor');
  290.             my($text_color) = param('text_color');
  291.             my($link_color) = param('link_color');
  292.             my($vlink_color) = param('vlink_color');
  293.             my($alink_color) = param('alink_color');
  294.  
  295.             my($htmlbody) =<<"__EOHTML__";
  296. <center>
  297. <p><font color="#ff0000">Some required fields have been left blank.</font></p>
  298. <p>Please enter your details to register:</p>
  299. <form action="regdb_updatecust.cgi" method="post">
  300. <input type="hidden" name="bgcolor" value="$bgcolor">
  301. <input type="hidden" name="text_color" value="$text_color">
  302. <input type="hidden" name="link_color" value="$link_color">
  303. <input type="hidden" name="vlink_color" value="$vlink_color">
  304. <input type="hidden" name="alink_color" value="$alink_color">
  305. <input type="hidden" name="code" value="$code">
  306. <input type="hidden" name="product" value="$product">
  307. <input type="hidden" name="version" value="$version">
  308. <input type="hidden" name="mversion" value="$mversion">
  309. <input type="hidden" name="language" value="$language">
  310. <input type="hidden" name="unlocker" value="$unlocker">
  311. <table border="1" bgcolor="#ffef90">
  312.         <tr>
  313.             <th align="left">Name</th>
  314.             <td colspan="3"><input type="text" size="40" name="customername" value="$customername"></td>
  315.         </tr>
  316.         <tr>
  317.             <th align="left">Company</th>
  318.             <td colspan="3"><input type="text" size="40" name="customercompany" value="$customercompany"></td>
  319.         </tr>
  320.         <tr>
  321.             <th align="left">Street</th>
  322.             <td colspan="3"><input type="text" size="40" name="customerstreet" value="$customerstreet"></td>
  323.         </tr>
  324.         <tr>
  325.             <th align="left">City</th>
  326.             <td colspan="3"><input type="text" size="40" name="customercity" value="$customercity"></td>
  327.         </tr>
  328.         <tr>
  329.             <th align="left">State</th>
  330.             <td><input type="text" size="20" name="customerstate" value="$customerstate"></td>
  331.             <th align="left">Zip</th>
  332.             <td><input type="text" size="8" name="customerzip" value="$customerzip"></td>
  333.         </tr>
  334.         <tr>
  335.             <th align="left">Country</th>
  336.             <td colspan="3"><input type="text" size="40" name="customercountry" value="$customercountry"></td>
  337.         </tr>
  338.         <tr>
  339.             <th align="left">Phone</th>
  340.             <td colspan="3"><input type="text" size="40" name="customerphone" value="$customerphone"></td>
  341.         </tr>
  342.         <tr>
  343.             <th align="left">Fax</th>
  344.             <td colspan="3"><input type="text" size="40" name="customerfax" value="$customerfax"></td>
  345.         </tr>
  346.         <tr>
  347.             <th align="left">Email</th>
  348.             <td colspan="3"><input type="text" size="40" name="customeremail" value="$customeremail"></td>
  349.         </tr>
  350.     </tr>
  351. </table>
  352. __EOHTML__
  353.  
  354.     if($coc == 1) {
  355.         $htmlbody .=<<"__EOHTML__";
  356. <br>
  357. <p>The product you are registering is an upgrade.  Please enter the Code from your
  358. previous version of ShopFactory.</p>
  359. <table border="1" bgcolor="#ffef90">
  360.     <tr>
  361.         <th align="left">Previous Code</th>
  362.         <td colspan="3"><input type="text" size="40" name="oldcode" value="$oldcode"></td>
  363.     </tr>
  364. </table>
  365. <input type="hidden" name="coc" value="1">
  366. __EOHTML__
  367.     }
  368.  
  369.     $htmlbody .=<<"__EOHTML__";
  370. <br><input type="submit" value="Submit"> <input type="reset" value="Reset"></center>
  371. </form>
  372. __EOHTML__
  373.  
  374.             &print_html("Enter Customer Details", $htmlbody);
  375.             exit;
  376.         }
  377.     }
  378. }
  379.  
  380. sub send_error_report {
  381.     my($error) = @_;
  382.     my($body);
  383.  
  384.     $body  = "$date\n";
  385.     $body .= "Error: $error\n";
  386.  
  387.     $body .= "\n----- Param Info -----\n\n";
  388.  
  389.     my (@names) = param;
  390.     for $name (@names) {
  391.         $body .= "$name = " . param($name) . "\n";
  392.     }
  393.  
  394.     $body .= "\n----- UA Info -----\n\n";
  395.     $body .= "Browser: $ENV{'HTTP_USER_AGENT'}\n";
  396.     $body .= "Remote Host: $ENV{'REMOTE_HOST'}\n";
  397.     $body .= "Remote Address: $ENV{'REMOTE_ADDR'}\n";
  398.  
  399.     &sendmail($email_alert,
  400.         "\"regdb_updatecust\" <$email_alert>",
  401.         "regdb_updatecust error!",
  402.         $body);
  403. }
  404.  
  405. sub print_html {
  406.     my($title, $desc) = @_;
  407.  
  408.     print "<html>\n<body";
  409.     &body_attributes;
  410.     print ">\n";
  411.  
  412.     print <<"__EOHTML__";
  413. <center>
  414. <table border="0" cellpadding="0" cellspacing="0" width="540">
  415.     <tr>
  416.         <td>
  417.             <font face="Arial, Helvetica">
  418.             <center><h4>$title</h4></center>
  419.             $desc
  420.             </font>
  421.         </td>
  422.     </tr>
  423. </table>
  424. </center>
  425. </body>
  426. </html>
  427. __EOHTML__
  428. }
  429.  
  430. sub body_attributes {
  431.     if (param('bgcolor')) {
  432.         print " bgcolor=\"" . param('bgcolor') . "\"";
  433.     }
  434.     if (param('link_color')) {
  435.         print " link=\"" . param('link_color') . "\"";
  436.     }
  437.     if (param('vlink_color')) {
  438.         print " vlink=\"" . param('vlink_color') . "\"";
  439.     }
  440.     if (param('alink_color')) {
  441.         print " alink=\"" . param('alink_color') . "\"";
  442.     }
  443.     if (param('text_color')) {
  444.         print " text=\"" . param('text_color') . "\"";
  445.     }
  446. }
  447.  
  448. sub sendmail {
  449.     my($to, $from, $subject, $body) = @_;
  450.  
  451.     open (MAIL, "|$mailprog -t -oi") || return 0;
  452.     print MAIL "To: $to\n";
  453.     print MAIL "From: $from\n";
  454.     print MAIL "Subject: $subject\n\n";
  455.     print MAIL "$body\n";
  456.     close MAIL;
  457.     return 1;
  458. }